package com.andy.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.andy.dao.IExamSubjectDAO;
import com.andy.dao.IExamUserDAO;
import com.andy.entity.ExamSubject;
import com.andy.entity.ExamUser;
import com.andy.vo.ExamSubjectVO;
import com.andy.vo.ExamUserVO;

@Service
@Transactional("transactionManager")
public class TeacherService implements ITeacherService {
	
	@Autowired
	private IExamSubjectDAO examSubjectDAO;
	
	@Autowired
	private IExamUserDAO examUserDAO;

	@Override
	public List<ExamSubjectVO> getAllSubjects() {
		List<ExamSubject> subjectList = examSubjectDAO.loadAll();
		List<ExamSubjectVO> resultList = new ArrayList<ExamSubjectVO>();
		for (ExamSubject subject : subjectList) {
			ExamSubjectVO vo = new ExamSubjectVO();
			BeanUtils.copyProperties(subject, vo);
			resultList.add(vo);
		}
		return resultList;
	}

	@Override
	public void addSubject(ExamSubjectVO paramVO) {
		ExamSubject subject = new ExamSubject();
		BeanUtils.copyProperties(paramVO, subject);
		examSubjectDAO.save(subject);
	}

	@Override
	public void editSubject(ExamSubjectVO paramVO) {
		ExamSubject subject = new ExamSubject();
		BeanUtils.copyProperties(paramVO, subject);
		examSubjectDAO.update(subject);
	}

	@Override
	public void delSubject(Integer subId) {
		ExamSubject subject = examSubjectDAO.get(subId);
		examSubjectDAO.delete(subject);
	}

	@Override
	public ExamSubjectVO getSubjectById(Integer subId) {
		ExamSubject subject = examSubjectDAO.get(subId);
		ExamSubjectVO vo = new ExamSubjectVO();
		BeanUtils.copyProperties(subject, vo);
		return vo;
	}

	@Override
	public void changeSubjectStatus(Integer subId, Integer status) {
		ExamSubject subject = examSubjectDAO.get(subId);
		subject.setStatus(status);
		examSubjectDAO.update(subject);
	}
	
	@Override
	public List<String> getAllUserBySubject(Integer subjectId) {
		return examSubjectDAO.getAllUserBySubject(subjectId);
	}
	
	/* (non-Javadoc) 获取所有学生的Service代码
	 * @see com.andy.service.ITeacherService#getAllStudent()
	 */
	@Override
	public List<ExamUserVO> getAllStudent() {
		List<ExamUser> userList = examUserDAO.getAllStudent();
		List<ExamUserVO> resultList = new ArrayList<ExamUserVO>();
		for (ExamUser user : userList) {
			ExamUserVO vo = new ExamUserVO();
			BeanUtils.copyProperties(user, vo);
			resultList.add(vo);
		}
		return resultList;
	}

	/* (non-Javadoc) 保存修改
	 * @see com.andy.service.ITeacherService#saveSubjectStudent(java.util.List, java.lang.Integer)
	 */
	@Override
	public void saveSubjectStudent(List<String> subjectStudents, Integer subjectId) throws RuntimeException {
		List<String> dbSubjectUserList = examSubjectDAO.getAllUserBySubject(subjectId);	// 获取当前科目关联的所有学生
		
		List<String> insertList = new ArrayList<String>();
		List<String> delList = new ArrayList<String>();
		
		// 1有，　２无　的情况
		// 判断哪些学生是新勾选上的
		for (String selectedUser : subjectStudents) {
			if (!dbSubjectUserList.contains(selectedUser)) {
				insertList.add(selectedUser);
			}
		}
		
		// １无　２有的情况
		// 判断哪些学生本来是勾选上的，现在去掉勾选的
		for (String selectedUser : dbSubjectUserList) {
			if (!subjectStudents.contains(selectedUser)) {
				delList.add(selectedUser);
			}
		}
		
		// insertList  分别保存
		examSubjectDAO.insertSubjectUsers(insertList, subjectId);
		// delList　分别删除
		examSubjectDAO.deleteSubjectUsers(delList, subjectId);
	
	}

}
